package weaver.interfaces.solead.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.solead.utils.SoleadUtil;
import weaver.interfaces.solead.wcf.WcfUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-01-07
 * @Description: SLD-SD-111_SAP订单下达流程触发接口生成SAP数据
 * @ModifyLog:
 **/

public class WeaDdxdAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        writeLog( ">>>>WeaDdxdAction" + requestid );
        JSONObject header = new JSONObject();
        JSONArray bodyList = new JSONArray();
        try {
            rs.executeQuery( "select fkfs,id,khdm,jhrq,ywydm,sqr,qtsm,jffs,psdz from " + tablename + " where requestid=?", requestid );
            rs.next();
            String mainid = Util.null2String( rs.getString( "id" ) );
            String khdm = Util.null2String( rs.getString( "khdm" ) );
            String jhrq = Util.null2String( rs.getString( "jhrq" ) );
            String ywydm = Util.null2String( rs.getString( "ywydm" ) );
            String qtsm = Util.null2String( rs.getString( "qtsm" ) );
            String sqr = Util.null2String( rs.getString( "sqr" ) );
            String fkfs = Util.null2String( rs.getString( "fkfs" ) );
            String jffs = Util.null2String( rs.getString( "jffs" ) );
            String psdz = Util.null2String( rs.getString( "psdz" ) );
            if ("0".equals( jffs )) {
                header.put( "U_SPDCode", "A043" );
            }
            if ("1".equals( jffs )) {
                header.put( "U_SPDCode", psdz );
            }
            ResourceComInfo comInfo = new ResourceComInfo();
            String lastname = comInfo.getLastname( sqr );
            String curdate = DateUtil.getCurrentDate();
            header.put( "CardCode", khdm );
            header.put( "NumAtCard", "" );
            header.put( "DocDate", WcfUtil.getFormatDate( curdate ) );
            header.put( "DocDueDate", WcfUtil.getFormatDate( jhrq ) );
            header.put( "SalesPersonCode", ywydm );
            header.put( "Comments", qtsm );
            header.put( "U_FWuser", lastname );
            header.put( "U_FWprocess", requestid );
            header.put( "U_PmtType", fkfs );
            writeLog( ">>>>>header=" + header.toJSONString() );
            rs.executeQuery( "select wlbm,bcxdsl,phwb,jg,sl,yt,zx,bhsdj,bz from " + tablename + "_dt1  where sfxdcp=0 and mainid=?", mainid );
            while (rs.next()) {
                String wlbm = Util.null2String( rs.getString( "wlbm" ) );
                String bcxdsl = Util.null2String( rs.getString( "bcxdsl" ) );
                String phwb = Util.null2String( rs.getString( "phwb" ) );
                String bhsdj = Util.null2String( rs.getString( "bhsdj" ) );
                String jg = Util.null2String( rs.getString( "jg" ) );
                String sl = Util.null2String( rs.getString( "sl" ) );
                String yt = Util.null2String( rs.getString( "yt" ) );
                yt = "".equals( yt ) ? "-" : yt;
                String zx = Util.null2String( rs.getString( "zx" ) );
                zx = "".equals( zx ) ? "--" : zx;
				String bz = Util.null2String( rs.getString( "bz" ) );
                JSONObject body = new JSONObject();
                body.put( "ItemCode", wlbm );
                body.put( "Quantity", bcxdsl );
                body.put( "U_BCHCode", phwb );
//                body.put( "UnitPrice", bhsdj );  舍弃
                body.put( "PriceAfterVAT", jg );
                body.put( "VatGroup", sl );
                body.put( "U_Usage", yt );
                body.put( "U_Attr", zx );
				body.put( "U_Remark", bz );
                writeLog( ">>>>>body=" + body.toJSONString() );
                bodyList.add( body );
            }
            writeLog( ">>>>>bodyList=" + bodyList.size() );
            String param = SoleadUtil.Ddxd_XMLContent( header, bodyList );
            Map<String, String> resultMap = WcfUtil.send( param, "solead", "17","" );
            String code = resultMap.get( "code" );
            String msg = resultMap.get( "msg" );
            if (!"0".equals( code )) {
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "Action接口异常," + msg );
                return Action.FAILURE_AND_CONTINUE;
            } else {
                rs.executeUpdate( "update " + tablename + " set sapddh=? where requestid=?", msg, requestid );
                rs.executeUpdate( "update " + tablename + "_dt1 set sapddh=? where mainid=?", msg, mainid );
                return Action.SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
